using System;
using System.Collections.Generic;
using System.Text;
using System.ServiceModel;
using WCFServer.ServiceContracts;

namespace WCFServer.ServiceImplementation
{
    [ServiceBehavior(Name = "Server", Namespace = "http://WCFServer.ServiceContracts/2007/11",InstanceContextMode=InstanceContextMode.Single)]
    public class Server : IServer
    {
        private List<WCFServer.BusinessLogic.PlayingPlayer> PlayerList;
        public Server()
        {
            PlayerList = new List<WCFServer.BusinessLogic.PlayingPlayer>();
        }
        
#region IServer Members
        
        CreatePlayerResponse IServer.CreatePlayer(CreateUserRequest request)
        {
            WCFServer.BusinessLogic.KlientManager manager = new WCFServer.BusinessLogic.KlientManager();
            manager.AddKlient(TranslateBetweenKlientAndPlayer.TranslatePlayerToKlient(request.Player));

            CreatePlayerResponse response = new CreatePlayerResponse();
            return response;
        }

        GetPlayerDetailsResponse IServer.GetPlayerDetails(GetPlayerDetailsRequest request)
        {
            WCFServer.BusinessLogic.KlientManager manager = new WCFServer.BusinessLogic.KlientManager();
            WCFServer.BusinessEntities.Klient klient = manager.getKlient(request.PlayerNick);

            GetPlayerDetailsResponse response = new GetPlayerDetailsResponse();
            response.Player = TranslateBetweenKlientAndPlayer.TranslateKlientToPlayer(klient);
            return response;
        }

        JoinGameResponse IServer.JoinGame(JoinGameRequest request)
        {
            JoinGameResponse response = new JoinGameResponse();
            try
            {
                foreach(WCFServer.BusinessLogic.PlayingPlayer player in PlayerList)
                {
                    if(player.Nick == request.UserNick)
                    {
                        response.result = WCFServer.DataContracts.JoinResult.JoinState.LoginTaken;
                        return response;
                    }
                }
                PlayerList.Add(new WCFServer.BusinessLogic.PlayingPlayer(request.UserNick,new WCFServer.BusinessLogic.Card(), 0, WCFServer.BusinessLogic.PlayingPlayer.PlayerState.NotPlaying));         
                response.result = WCFServer.DataContracts.JoinResult.JoinState.Joined;
            }
            catch
            {
                response.result = WCFServer.DataContracts.JoinResult.JoinState.Faulted;
            }
            return response;
        }

        LeaveGameResponse IServer.LeaveGame(LeaveGameRequest request)
        {
            LeaveGameResponse response = new LeaveGameResponse();
            try
            {
                foreach (WCFServer.BusinessLogic.PlayingPlayer player in PlayerList)
                {
                    if (player.Nick == request.UserNick)
                    {
                        PlayerList.Remove(player);
                        response.result = WCFServer.DataContracts.LeaveResult.LeveState.Left;
                        break;
                    }
                }
            }
            catch
            {
                response.result = WCFServer.DataContracts.LeaveResult.LeveState.Foulted;
            }
            return response;
        }

        UserPlaceCardResponse IServer.UserPlaceCard(UserPlaceCardRequest request)
        {
            UserPlaceCardResponse response = new UserPlaceCardResponse();
            try
            {
                foreach (WCFServer.BusinessLogic.PlayingPlayer player in PlayerList)
                {
                    if (player.Nick == request.UserNick)
                    {
                        player.round++;
                        bool finished_round = true;
                        player.LastCard = TranslateBetweenCardAndCard.TranslateCardToCard(request.Card);
                        for (int i = 0; i < PlayerList.Count-1; i++)
                        {
                            if (PlayerList[i].round != PlayerList[i + 1].round)
                            {
                                finished_round = false;
                                response.GameResult = WCFServer.DataContracts.GameResult.GameState.InProgress;
                                response.PlayerResult = WCFServer.DataContracts.PlayerResult.PlayerState.NotKnown;
                                break;
                            }
                            if (finished_round)
                            {
                                PlayerList.Sort(new WCFServer.BusinessLogic.PlayingPlayer.playerCardsComparer());
                                PlayerList[0].State = WCFServer.BusinessLogic.PlayingPlayer.PlayerState.Won;
                                for (int j = 1; j < PlayerList.Count; j++)
                                {
                                    PlayerList[j].State = WCFServer.BusinessLogic.PlayingPlayer.PlayerState.Lost;
                                }
                                response.PlayerResult = WCFServer.DataContracts.PlayerResult.PlayerState.Winner;
                                response.GameResult = WCFServer.DataContracts.GameResult.GameState.InProgress;
                                break;
                            }
                        }
                        break;
                    }
                }
            }
            catch
            {
                response.GameResult = WCFServer.DataContracts.GameResult.GameState.Faulted;
                response.PlayerResult = WCFServer.DataContracts.PlayerResult.PlayerState.NotKnown;
            }
            return response;
        }

        GetWinnerResponse IServer.GetWinner(GetWinnerRequest request)
        {
            GetWinnerResponse response = new GetWinnerResponse();
            try
            {
                foreach (WCFServer.BusinessLogic.PlayingPlayer player in PlayerList)
                {
                    if (player.State == WCFServer.BusinessLogic.PlayingPlayer.PlayerState.Won)
                    {
                        response.WiinerNick = player.Nick;
                        break;
                    }
                }
            }
            catch
            {
                response.WiinerNick = null;
            }
            return response;
        }

        public GetGameWinnerResponse GetGameWinner(GetGameWinnerRequest request)
        {
            GetGameWinnerResponse response = new GetGameWinnerResponse();
            try
            {
                List<WCFServer.BusinessLogic.PlayingPlayer> tmp = new List<WCFServer.BusinessLogic.PlayingPlayer>(PlayerList);
                tmp.Sort(new WCFServer.BusinessLogic.PlayingPlayer.playerPointsComparer());

                response.PlayerNick = tmp[tmp.Count - 1].Nick;
            }
            catch
            {
                response.PlayerNick = null;
            }
            return response;
        }

        #endregion
    }
}
